package com.ay.selfservicemachine.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import androidx.core.content.FileProvider;

import com.ay.selfservicemachine.BaseApplication;

import java.io.File;


public class AppUtils {



    public static String getFileProviderAuthorities(){
        return BaseApplication.getAppContext().getPackageName() + ".fileprovider";
    }

    /**
     * 显示软键盘
     */
    public static void showSoftInput(Context context,View view){
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInputFromInputMethod(view.getWindowToken(),InputMethodManager.SHOW_FORCED);
    }

    /**
     * 隐藏软键盘
     */
    public static void hideSoftInput(Context context, View view) {
        if (view != null) {
            InputMethodManager inputmanger = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 隐藏软键盘
     */
    public static void hideSoftInput(Context context, Window window) {
        View inputView = window.peekDecorView();
        if (inputView != null) {
            hideSoftInput(context,inputView);
//            InputMethodManager inputmanger = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
//            inputmanger.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
        }
    }




    /**
     * 获取图片的全路径
     * @param urlKey
     * @param pictureSize
     * @return
     */
    public static String loadPictureUrlOrSize(String urlKey,String pictureSize) {
        String urlValue = urlKey;
        if(!TextUtils.isEmpty(urlKey) && !"null".equalsIgnoreCase(urlKey)){
            if(!urlKey.startsWith("http")  &&!urlKey.startsWith("https") && !urlKey.startsWith("/storage")){
//                urlValue = BuildConfig.API_ALIYUN_OSS_HEAD + urlKey;
            }
        }
        if(!TextUtils.isEmpty(pictureSize) && !"null".equalsIgnoreCase(pictureSize)){
            urlValue = urlValue + pictureSize;
        }
        return urlValue;
    }


    public static int getVersionCode(Context context) {
        int versioncode = 0;
        try {
            versioncode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (Exception v) {
        }
        return versioncode;
    }

    public static String getVersionName(Context context) {
        String versionname = "";
        try {
            versionname = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (Exception v) {
        }
        return versionname;
    }

    public static void InstallApk(Context context, File file) {
        if (context == null) {
            return;
        }
        String authority = BaseApplication.getAppContext().getPackageName() + ".fileprovider";
        Uri apkUri = FileProvider.getUriForFile(context, authority, file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        if (Build.VERSION.SDK_INT >= 24) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }



}
